﻿package com.usfuture.customComponent{

	import flash.display.MovieClip;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextLineMetrics;
	import flash.events.MouseEvent;

	public class ButtonStyle1 extends MovieClip {

		private var txt:TextField = new TextField();
		private var sprite:Sprite = new Sprite();
		private var fontWidth:Number;
		private var fontHeight:Number;
		private var paddingLeft:Number;
		private var paddingTop:Number=2;
		private var minPaddingLeft:Number=5;
		private var btnWidth:int;
		private var btnHeight:int=20;
		private var minBtnWidth:Number=80;

		public function ButtonStyle1(text:String) {
			configureLabel(text);
			configureShape();
			measure();
			addChild(txt);
			addChild(sprite);
			updateDisplay(btnWidth,btnHeight);
			//addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler)
		}

		private function configureLabel(text:String):void {
			var textFormat:TextFormat = new TextFormat();
			textFormat.font='宋体';
			textFormat.size=12;
			textFormat.color=0x000000;
			txt.defaultTextFormat=textFormat;
			txt.selectable=false;
			txt.text=text;
		}

		private function configureShape():void {
			sprite.buttonMode=true;
			sprite.useHandCursor=true;
		}

		private function measure():void {
			var lineMetrics:TextLineMetrics=txt.getLineMetrics(0);
			fontWidth=lineMetrics.width;
			fontHeight=lineMetrics.leading+lineMetrics.descent+lineMetrics.ascent;
			//计算宽度
			if (fontWidth+minPaddingLeft*2<minBtnWidth) {
				paddingLeft = (minBtnWidth-fontWidth)/2;
				btnWidth=minBtnWidth;
			} else {
				paddingLeft=minPaddingLeft;
				btnWidth=int(fontWidth+minPaddingLeft*2);
			}
			//计算高度
			//btnHeight = int(Math.floor(paddingTop*2+fontHeight));
			txt.x=paddingLeft;
			txt.y=paddingTop;
			sprite.width=btnWidth;
			sprite.height=btnHeight;
		}


		private function updateDisplay(w:Number,h:Number):void {

			var matr:Matrix=new Matrix();
			matr.createGradientBox(20,20,(90/180)*Math.PI,0,0);
			graphics.beginGradientFill(GradientType.LINEAR,[0xeef2f5,0xa4bfdd],[1,1],[0,127],matr,SpreadMethod.PAD);
			graphics.lineStyle(1,0x5a6b73,1);
			graphics.drawRoundRect(0,0,w,h,2,2);
			graphics.endFill();
		}
		
		private function mouseOverHandler(event:MouseEvent):void{
			trace(currentLabel)
		}

	}

}